package com.extension.company.common.widget.popuwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.extension.company.admin.R;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/7/5 08:17
 * 功能：自定义分享的popwindow
 */
public class SharePw extends PopupWindow implements View.OnClickListener {
	private Activity activity;
	private clikcType click;
	private LinearLayout shareLayoutTop;
	private LinearLayout weixin, weixinCircle, linkCopy;
	private TextView close;

	public SharePw(Activity activity, clikcType clickType) {
		this.activity = activity;
		this.click = clickType;
		View view = LayoutInflater.from(activity).inflate(R.layout.share_popwindow, null);
		initView(view);
		initClicck();
		setContentView(view);
		setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
		setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x00000000); //半透明色
		setBackgroundDrawable(dw);
		setAnimationStyle(R.style.popwin_anim_style); // pop进入退出的动画
		setOutsideTouchable(true);

		view.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int topY = shareLayoutTop.getTop();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (topY > event.getY()) {
						dismiss();
					}
				}

				return true;
			}
		});
	}

	/**
	 * 初始化
	 */
	private void initView(View view) {

		shareLayoutTop = view.findViewById(R.id.share_pw_top);
		weixin = view.findViewById(R.id.weixin);
		weixinCircle = view.findViewById(R.id.weixin_circle);
		linkCopy = view.findViewById(R.id.link_copy);
		close = view.findViewById(R.id.share_cancle);
	}

	private void initClicck() {
		weixin.setOnClickListener(this);
		weixinCircle.setOnClickListener(this);
		linkCopy.setOnClickListener(this);
		close.setOnClickListener(this);
		setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				backgroundAlpha(1f);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.weixin:
				click.callBackType("WEIXIN");
				break;
			case R.id.weixin_circle:
				click.callBackType("WEIXIN_CIRCLE");
				break;
			case R.id.link_copy:
				click.callBackType("COPY_LINK");
				break;
			case R.id.share_cancle:
				dismiss();
			default:
				break;
		}
	}


	public void show() {
		if (!isShowing()) {
			showAtLocation(((ViewGroup) activity.findViewById(android.R.id
					.content)).getChildAt(0), Gravity.CENTER, 0, 0); // 在布局中第一个viewGroup
			backgroundAlpha(0.7f);
		} else {
			dismiss();
		}
	}

	public void showPopupWindow(View parent) {
		if (!isShowing()) {
			showAsDropDown(parent, parent.getLayoutParams().width / 2, 20);
		} else {
			dismiss();
		}
	}

	private void backgroundAlpha(float bgAlpha) {
		if (bgAlpha == 1) {
			//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
			activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		} else {
			//此行代码主要是解决在华为手机上半透明效果无效的bug
			activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0~1.0
		activity.getWindow().setAttributes(lp); //act 是上下文context
	}

	public void hideSoftInputView() {
		InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (activity.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
			if (activity.getCurrentFocus() != null) {
				manager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

	public interface clikcType {
		void callBackType(String shareMedia);
	}


}
